﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoMapper;
using TestAutoMapperPorject.DTO;
using TestAutoMapperPorject.Modal;

namespace TestAutoMapperPorject
{
    [TestClass]
    public class UnitTest1
    {
        [AssemblyInitialize()]
        public static void AssemblyInit(TestContext context)
        {

        }

        [TestMethod]
        public void TestFromMember()
        {
            Mapper.Reset();
            Mapper.CreateMap<Product, ProductDataObject>()
                .ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id))
                .ForMember(d => d.Name, opt => opt.MapFrom(s => s.Name));

            Product m = new Product() { Id = Guid.NewGuid(), Name = "A Product" };
            var dto = Mapper.Map<Product, ProductDataObject>(m);
            Assert.IsNotNull(dto);
            Assert.AreEqual(m.Id, dto.Id);
            Assert.AreEqual(m.Name, dto.Name);
        }

        [TestMethod]
        public void TestFromMemberAuto()
        {
            Mapper.Reset();
            Mapper.CreateMap<Product, ProductDataObject>();
            Mapper.AssertConfigurationIsValid();
            Product m = new Product() { Id = Guid.NewGuid(), Name = "A Product" };
            var dto = Mapper.Map<Product, ProductDataObject>(m);
            Assert.IsNotNull(dto);
            Assert.AreEqual(m.Id, dto.Id);
            Assert.AreEqual(m.Name, dto.Name);
        }

        [TestMethod]
        public void TestCustomConvert()
        {
            Mapper.Reset();
            Mapper.CreateMap<int, string>().ConvertUsing(s=>s.ToString());

            int src = 100;
            var des = Mapper.Map<String>(src);
            Assert.AreEqual("100", des);
        }

        [TestMethod]
        public void TestPropertyMapping()
        {
            Mapper.Reset();
            Mapper.CreateMap<Order, OrderDataObject>()
                .ForMember(d => d.CustomerId, opt => opt.MapFrom(s => s.Customer.Id))
                //使用ResolveUsing和使用MapFrom差不多，方法签名不同，但是在调用方式的表现上完全一致
                .ForMember(d => d.CustomerName, opt => opt.ResolveUsing(s => s.Customer.Name))      
                .ForMember(d => d.CustomerTel, opt => opt.MapFrom(s => s.Customer.Tel))

                .ForMember(d => d.OrderId, opt => opt.MapFrom(s => s.Id))
                .ForMember(d => d.ShippingDate, opt => opt.MapFrom(s => s.ShippingDate));
            Mapper.AssertConfigurationIsValid();

            Order o = new Order() { 
                Id = Guid.NewGuid(), 
                Customer = new Customer() { Id=Guid.NewGuid(),Name="A Customer",Tel="131"}, 
                ShippingDate=DateTime.Now};
            var dto = Mapper.Map<OrderDataObject>(o);
            Assert.IsNotNull(dto);
            Assert.AreEqual(o.Id, dto.OrderId);
            Assert.AreEqual(o.ShippingDate, dto.ShippingDate);
            Assert.AreEqual(o.Customer.Id, dto.CustomerId);
            Assert.AreEqual(o.Customer.Name, dto.CustomerName);
            Assert.AreEqual(o.Customer.Tel, dto.CustomerTel);

        }
    }
}
